package com.gxuwz.Market.business.action.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.Market.business.entity.ServiceAcademy;
import com.gxuwz.Market.business.entity.ServiceMajor;
import com.gxuwz.Market.business.service.IServiceAcademyService;
import com.gxuwz.Market.business.service.IServiceMajorService;
import com.gxuwz.core.pagination.Result;
import com.gxuwz.core.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ServiceMajorAction extends BaseAction implements Preparable, ModelDriven<ServiceMajor> {

	protected static final String LIST_JSP = "/WEB-INF/page/web/ServiceMajor_list.jsp";  
	protected static final String ADD_JSP = "/WEB-INF/page/web/ServiceMajor_add.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/web/ServiceMajor_edit.jsp";
	
	@Autowired
	private IServiceMajorService serviceMajorService;
	@Autowired
	private IServiceAcademyService serviceAcademyService;
	
	
	private ServiceMajor serviceMajor;
	private Result<ServiceMajor> pageResult;
	private List<ServiceAcademy> academyList;
	private Map<String,String> majorMap = new HashMap<String, String>();
	
	
	
	@Override
	public void prepare() throws Exception {
		if(null == serviceMajor){
			serviceMajor = new ServiceMajor();
		}
	}

	@Override
	public ServiceMajor getModel() {
		return serviceMajor;
	}
	
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
	
	/**
	 * 打开列表页面
	 * @return
	 */
	public String openList(){
		pageResult = serviceMajorService.find(serviceMajor, getPage(), getRow());
		
		try{
			for(ServiceMajor major:pageResult.getData()){
				ServiceAcademy academy = serviceAcademyService.findByProperty("academyId", major.getAcademyId()).get(0);
				majorMap.put(major.getMajorId(), academy.getAcademyName());
			}
		}catch(Exception e){
			System.out.println(e);
		}
		academyList = serviceAcademyService.findAll();
	

		setForwardView(LIST_JSP);
		return SUCCESS;
	}

	/**
	 * 删除
	 * @return
	 */
	public String delete() {
		serviceMajorService.delete(serviceMajor.getId());
		serviceMajor.setId(null);
		return openList();
	}
	
	
	
	/**
	 * 打开修改页面
	 * @return
	 */
	public String openEdit(){
		serviceMajor = serviceMajorService.findById(serviceMajor.getId());
        academyList = serviceAcademyService.findAll();
		setForwardView(EDIT_JSP);
		return SUCCESS;
	}

	public String openAdd() {
		academyList = serviceAcademyService.findAll();
		
		
		
		setForwardView(ADD_JSP);
		
		return SUCCESS;
	}
	
	public String add() {
		serviceMajorService.add(serviceMajor);
		serviceMajor=new ServiceMajor();
		return openList();
	}
	/**
	 * 修改
	 * @return
	 */
	public String edit(){
		if(null != serviceMajor){
			serviceMajorService.update(serviceMajor);
		}
		serviceMajor =new ServiceMajor();
		return openList();
	}
	
	public ServiceMajor getServiceMajor() {
		return serviceMajor;
	}

	public void setServiceMajor(ServiceMajor serviceMajor) {
		this.serviceMajor = serviceMajor;
	}

	public Result<ServiceMajor> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<ServiceMajor> pageResult) {
		this.pageResult = pageResult;
	}

	public List<ServiceAcademy> getAcademyList() {
		return academyList;
	}

	public void setAcademyList(List<ServiceAcademy> academyList) {
		this.academyList = academyList;
	}

	public Map<String, String> getMajorMap() {
		return majorMap;
	}

	public void setMajorMap(Map<String, String> majorMap) {
		this.majorMap = majorMap;
	}







	
	
	
}
